package com.example.springmvcinterview.understandAOP;

import org.aopalliance.intercept.MethodInterceptor;
import org.aopalliance.intercept.MethodInvocation;
import org.springframework.aop.AfterReturningAdvice;
import org.springframework.aop.MethodBeforeAdvice;
import org.springframework.aop.ThrowsAdvice;

import java.lang.reflect.Method;

/**
 * @Author : 叶荻风
 * @create : 2024/12/18 15:31
 */

public class UserServiceAspect implements MethodBeforeAdvice {

    @Override
    public void before(Method method, Object[] args, Object target) throws Throwable {
        System.out.println("前置通知。。。");
    }


    public void afterReturning(Object returnValue, Method method, Object[] args, Object target) throws Throwable {
        System.out.println("后置通知");
    }


    public Object invoke(MethodInvocation invocation) throws Throwable {
        System.out.println("环绕通知前。。。");
        Object proceed = invocation.proceed();
        System.out.println("环绕通知后。。。");
        return proceed;
    }

    // 异常通知方法名必须为afterThrowing
    public void afterThrowing(Exception ex) {
        System.out.println("产生了异常：" + ex.getMessage());
    }
}